<template>
  <div>
    <el-row>
      <el-col :span="24">
        <div class="grid-content bg-purple">
          <FormPanel ref="formPanel" :list="formList" :cols="2" :labelWidth="250" :formType="type" :isSubmitBtn="false" @formChange="formChangeHandle" />
        </div>
      </el-col>
    </el-row>

    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <el-button v-if="type !== 'show'" type="primary" @click="saveDialog">{{ btnText }}</el-button>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { findAllRepair, updateBasicParam } from '@/api/repair/basicParameters';

export default {
  name: 'PartModal',
  mixins: [dictionary],
  props: {
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.conf = { add: '保 存', edit: '修 改', show: '明细' };
    this.saveData = null;

    return {
      formList: this.createFormList()
    };
  },
  computed: {
    btnText() {
      return this.conf[this.type];
    }
  },
  created() {
    this.init();
  },
  mounted() {
    this.queryDefaultPa();
  },
  methods: {
    init() {
      if (this.type !== 'add') {
        // 更新接口需要的记录 ID
        this.recordId = this.rowData.partNo;
        this.formList.forEach(x => {
          x.initialValue = this.rowData[x.fieldName];
        });
      }
    },
    createFormList() {
      return [
        {
          type: 'SELECT',
          label: '缺省仓库',
          fieldName: '1',
          placeholder: '请选择',
          itemList: this.createDictList('1004')
        },
        {
          type: 'CHECKBOX',
          label: '仓库关单',
          fieldName: '2'
        },
        {
          type: 'SELECT',
          label: '内部领用价格类型',
          fieldName: '3',
          placeholder: '请选择',
          itemList: this.createDictList('1004')
        },
        {
          type: 'CHECKBOX',
          label: '发料时必须指定领料人',
          fieldName: '4'
        },
        {
          type: 'SELECT',
          label: '发料打印显示顺序',
          fieldName: '5',
          placeholder: '请选择',
          itemList: this.createDictList('1004')
        },
        {
          type: 'CHECKBOX',
          label: '发料单打印配件库存',
          fieldName: '6'
        },
        {
          type: 'INPUT',
          label: '维修税率',
          fieldName: '7'
        },
        {
          type: 'CHECKBOX',
          label: '是否自动打印发料单',
          fieldName: '8'
        }
      ];
    },

    formChangeHandle(val) {
      this.saveData = val;
    },

    async saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();

      let res = await updateBasicParam(this.saveData);
      if (res.resultCode === 200) {
        this.test();
      }
    },
    async queryDefaultPa() {
      let res = await findAllRepair();

      this.formList[1].initialValue = res.data.parts_internalCollarPrice;
    },

    closeDialog(param) {
      this.$refs.formPanel.RESET_FORM();
      this.queryDefaultPa();
    },
    test() {
      this.$notify({
        title: '成功',
        message: '成功',
        type: 'success'
      });
    }
  }
};
</script>

<style lang="less" scoped>
p {
  background-color: #d3dce6;
  height: 20px;
  line-height: 20px;
  margin-bottom: 10px;
}
.elspan {
  margin-bottom: 70px;
}
span {
  top: 50px;
  color: red;
}

.el-row {
  margin-bottom: 50px;
}
</style>
